package com.lx.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * batch入门 -> Hello World
 * @author lixing
 * @date 2021/7/11 21:39
 */
@Configuration
//自动帮你补全一些重要的有关batch工作时的属性依赖(可以直接加在启动类上)
@EnableBatchProcessing
@Slf4j
public class JobConfiguration1 {

    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Bean
    public Job helloWorldJolb(){
        return jobBuilderFactory.get("helloWorldJolad")
                .start(step1())
                .next(step2())
                .build();
    }

    @Bean
    public Step step2() {
        return stepBuilderFactory.get("setp2222")
                .tasklet((stepContribution, chunkContext) -> {
                    log.info("aaaaaaaaaaaaaaaaa");
                    System.out.println("结束");
                    return RepeatStatus.FINISHED;
                }).build();
    }

    @Bean
    public Step step1() {
        return stepBuilderFactory.get("setp111")
                .tasklet(new Tasklet() {
                    @Override
                    public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                        try {
                            Thread.sleep(120);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        log.info("bbbbbbbbbbbbbbbbbbbbbbbbbbb");
                        System.out.println("Hello SpringBoot——Batch");
                        return RepeatStatus.FINISHED;
                    }
                }).build();
    }

}
